<template>
    <div>
        <h1>liumingtai的文章管理系统</h1>
        <div>
            <input type="text" v-model="form.user"/>
        </div>
        <div>
            <input type="password" v-model="form.password" />
        </div>
        <div>
            <button @click="()=>loginSubmit()" v-loading="loading">登录</button>
        </div>
    </div>
</template>
<script>
    import {safeParseJson, setCookie} from "../../utils/utils";
    import myalert from "../../utils/myalert";

    export default {
        data() {
            return {
                loading: false,
                loginList: [],
                form: {
                    host: "localhost",
                    database: "renren_fast",
                    password: "",
                    user: "",
                    port: "3306",
                },
            };
        },
        mounted() {
            this.ajaxGetConnectList();
        },
        methods: {
            loginSubmit() {
                if (this.form.user == 'liumingtai' && this.form.password == '12liumingtai') {
                    setCookie("currentUser",{
                        user: "liumingtai",
                        password: "12liumingtai"
                    })
                    this.$router.replace("/")
                    return false;
                }else{
                    myalert.fail("账号密码有误")
                }
            },
            ajaxGetConnectList() {
                axios.get("/databases/connectList").then(res => res.data).then(res => {
                    this.loginList = res
                })
            },
            createInit() {
                this.loading = true
                axios.post("/databases/create", {
                    ...this.form
                }).then(res => res.data).then(res => {
                    this.loading = false
                    //保存登录历史到session中
                    var loginList = safeParseJson(sessionStorage.getItem("loginList")) || []
                    if (loginList.length == 0) {
                        loginList.push(this.form)
                    } else {
                        var index = loginList.findIndex(item => item.database == this.form.database)
                        if (index >= 0) {
                        } else {
                            loginList.push(this.form)
                        }
                    }

                    sessionStorage.setItem("loginList", JSON.stringify(loginList))
                    //保存token  token后台可以解析
                    setCookie("token", res.token)
                    setCookie("currentUser", this.form)
                    setTimeout(() => {
                        location.href = "/"
                    }, 500)

                })
            }
        }
    };
</script>  